var ByteBuf = Java.type("io.netty.buffer.ByteBuf");
var Channel = Java.type("io.netty.channel.Channel");
var ChannelHandlerContext = Java.type("io.netty.channel.ChannelHandlerContext");
var ChannelHandlerAdapter = Java.type("io.netty.channel.ChannelHandlerAdapter");

var Charset = Java.type("java.nio.charset.Charset");
var HashMap = Java.type("java.util.HashMap");
var ArrayList = Java.type("java.util.ArrayList");
var CharArray = Java.type("char[]");
var ByteArray = Java.type("byte[]");
var JavaString = Java.type("java.lang.String");
var File = Java.type("java.io.File");
var PrintWriter = Java.type("java.io.PrintWriter");
var StringReader = Java.type("java.io.StringReader");
var StringWriter = Java.type("java.io.StringWriter");
var System = Java.type("java.lang.System");

var FileUtils = Java.type("org.apache.commons.io.FileUtils");
var ComboPooledDataSource = Java
		.type("com.mchange.v2.c3p0.ComboPooledDataSource");
var JavaJSON = Java.type("com.alibaba.fastjson.JSON");

var StringParser = Java.type("org.hxzon.util.StringParser");
var Data = Java.type("org.hxzon.util.Data");
var Dt = Java.type("org.hxzon.util.Dt");
var Db = Java.type("org.hxzon.util.db.Db");

var AbstractAdmCmdImpl = Java
		.type("org.hxzon.rpggame.core.impl.AbstractAdmCmdImpl");
var StringBuilder = Java.type("java.lang.StringBuilder");
var AdmAccount = Java.type("org.hxzon.rpggame.core.AdmAccount");
var Def = Java.type("org.hxzon.rpggame.core.Def");
var ScriptEngineUtil = Java.type("org.hxzon.rpggame.util.ScriptEngineUtil");
var Mail = Java.type("org.hxzon.rpggame.bean.Mail");

//==========
var encoding = "UTF-8";
var charset = Charset.forName(encoding);
var clientServerPort = 8888;
var adminServerPort = 9999;
var MaxFrameLength = 10240;//10k
var LengthFieldLength = 4;

var logger=Java.type("org.slf4j.LoggerFactory").getLogger("rpggame");

function readString( byteBuf) {
    try {
        var len = byteBuf.readShort();
        var strBytes = new ByteArray(len);
        byteBuf.readBytes(strBytes);
        return new JavaString(strBytes, encoding);
    } catch (e) {
        throw e;
    }
}

function writeString(byteBuf, str) {
    try {
        var strBytes = str.getBytes(encoding);
        byteBuf.writeShort(strBytes.length);
        byteBuf.writeBytes(strBytes);
    } catch (e) {
        throw e;
    }
}

function readCmd(byteBuf) {
    return byteBuf.readInt();
}

function writeCmd(byteBuf, cmd) {
    byteBuf.writeInt(cmd);
}

function readLength(byteBuf) {
    return byteBuf.readInt();
}

//==========
/* clientCmds */
var clientCmds = new HashMap();

function defClientCmd(cmd, fn) {
	clientCmds.put(cmd, fn);
}

function clientCmdHandler(byteBuf, ctx) {
	var cmd = readCmd(byteBuf);
	var handler = clientCmds.get(cmd);
	if (handler) {
		handler(byteBuf, ctx);
	}
}

/* client channels */
var clientChannels = new HashMap();

function addClient(channel, userAccount) {
	clientChannels.put(channel, userAccount);
}

function getClient(channel) {
	return clientChannels.get(channel);
}

function removeClient(channel) {
	clientChannels.remove(channel);
}

/* def */
var goDefs = new HashMap();

function getDef(did) {
	return goDefs.get(did);
}

function addDef(def) {
	goDefs.put(def.getDid(), def);
}
